package com.csun.cmny.provider.service.impl;

import com.csun.cmny.base.dto.LoginAuthDto;
import com.csun.cmny.base.enums.ErrorCodeEnum;
import com.csun.cmny.core.support.BaseService;
import com.csun.cmny.provider.dao.DmcCsunDoorLocationMapper;
import com.csun.cmny.provider.dao.DmcFallRadarLocationMapper;
import com.csun.cmny.provider.exceptions.DmcBizException;
import com.csun.cmny.provider.model.domain.DmcCsunDoorLocation;
import com.csun.cmny.provider.model.domain.DmcDevice;
import com.csun.cmny.provider.model.domain.DmcFallRadarLocation;
import com.csun.cmny.provider.model.dto.csundoor.BindCsunDoorLocationDto;
import com.csun.cmny.provider.model.dto.csundoor.CsunDoorPageQuery;
import com.csun.cmny.provider.model.dto.fallradar.BindFallRadarLocationDto;
import com.csun.cmny.provider.model.dto.fallradar.FallRadarPageQuery;
import com.csun.cmny.provider.model.dto.propertyManagement.SelectDeviceDataDto;
import com.csun.cmny.provider.model.enums.device.DmcDeviceTypeEnum;
import com.csun.cmny.provider.model.vo.csundoor.DmcCsunDoorVo;
import com.csun.cmny.provider.model.vo.fallradar.DmcFallRadarVo;
import com.csun.cmny.provider.model.vo.propertyManagement.PropertyDataVo;
import com.csun.cmny.provider.service.DmcCsunDoorLocationService;
import com.csun.cmny.provider.service.DmcDeviceAlertService;
import com.csun.cmny.provider.service.DmcDeviceService;
import com.csun.cmny.provider.service.DmcFallRadarLocationService;
import com.csun.cmny.util.PubUtils;
import com.csun.cmny.util.PublicUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2020-06-18 10:10
 * @Description:
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class DmcFallRadarLocationServiceImpl extends BaseService<DmcFallRadarLocation> implements DmcFallRadarLocationService {

    @Resource
    private DmcFallRadarLocationMapper dmcFallRadarLocationMapper;
    @Resource
    private DmcDeviceService dmcDeviceService;
    @Resource
    private DmcDeviceAlertService dmcDeviceAlertService;

    /**
     * bind csun door location
     *
     * @param bindFallRadarLocationDto bind csun door location dto
     * @param loginAuthDto            login auth dto
     * @return the int.
     */
    @Override
    public Integer bindFallRadarLocation(BindFallRadarLocationDto bindFallRadarLocationDto, LoginAuthDto loginAuthDto) {

        // 1.校验设备是否是雷达
        DmcDevice dmcDevice = dmcDeviceService
                .checkDeviceIsRadarByDeviceId(bindFallRadarLocationDto.getDeviceId());
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610073);
        }

        // 2.校验设备是否已经被绑定
//        DmcFallRadarVo fallRadar = getFallRadarByDeviceId(bindFallRadarLocationDto.getDeviceId());
//        if (fallRadar==null || fallRadar.isBind()){
//            throw new DmcBizException(ErrorCodeEnum.DMC100610074);
//        }
        DmcFallRadarLocation dmcFallRadarLocationQuery = dmcFallRadarLocationMapper
                .selectByPrimaryKey(bindFallRadarLocationDto.getDeviceId());
        if (PublicUtil.isNotEmpty(dmcFallRadarLocationQuery)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610074);
        }

        // 6.添加绑定
        DmcFallRadarLocation dmcFallRadarLocation = new DmcFallRadarLocation();
        BeanUtils.copyProperties(bindFallRadarLocationDto, dmcFallRadarLocation);
        dmcFallRadarLocation.setUpdateInfo(loginAuthDto);

        return dmcFallRadarLocationMapper.insertSelective(dmcFallRadarLocation);
    }


    @Override
    public List<PropertyDataVo> selectDeviceData(SelectDeviceDataDto selectDeviceDataDto) {
        return dmcFallRadarLocationMapper.selectDeviceData(selectDeviceDataDto);
    }

    /**
     * break csun button location
     *
     * @param deviceId device id
     * @return the int.
     */
    @Override
    public Integer breakFallRadarLocation(Long deviceId) {

        // 1.判断设备Id是否存在
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsRadarByDeviceId(deviceId);
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610073);
        }
        // 2.校验设备是否已经被绑定
        DmcFallRadarLocation dmcFallRadarLocationQuery = dmcFallRadarLocationMapper
                .selectByPrimaryKey(deviceId);
        if (PublicUtil.isEmpty(dmcFallRadarLocationQuery)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610032, deviceId);
        }
        // 3.解绑紧急按钮
        Integer result = dmcFallRadarLocationMapper.deleteByPrimaryKey(deviceId);
        if (result > 0) {
            dmcDeviceAlertService.deleteByDeviceId(deviceId);
        }

        return result;
    }

    /**
     * query fall radar list with page
     * @param fallRadarPageQuery csun door page query
     * @return thee info.
     */
    @Override
    public PageInfo queryGroupFallRadarListWithPage(FallRadarPageQuery fallRadarPageQuery) {
        PageHelper.startPage(fallRadarPageQuery.getPageNum(), fallRadarPageQuery.getPageSize());
        List<DmcFallRadarVo> dmcFallRadarVoList = dmcFallRadarLocationMapper
                .queryFallRadarListWithPage(fallRadarPageQuery);

        return new PageInfo<>(dmcFallRadarVoList);
    }

    /**
     * get csun door by device id
     *
     * @param deviceId device id
     * @return dmc csun door vo
     */
    @Override
    public DmcFallRadarVo getFallRadarByDeviceId(Long deviceId) {
        FallRadarPageQuery fallRadarPageQuery = new FallRadarPageQuery();
        fallRadarPageQuery.setDeviceId(deviceId);
        fallRadarPageQuery.setDeviceType(DmcDeviceTypeEnum.FALL_RADAR.getIndex());
        PageHelper.startPage(1, 1);
        List<DmcFallRadarVo> dmcFallRadarVoList = dmcFallRadarLocationMapper
                .queryFallRadarListWithPage(fallRadarPageQuery);
        if (PubUtils.isNull(dmcFallRadarVoList)) {
            return null;
        }
        return dmcFallRadarVoList.get(0);
    }

    /**
     * get csun door by device id
     *
     * @param deviceNo device no
     * @return dmc csun door vo
     */
    @Override
    public DmcFallRadarVo getFallRadarByDeviceNo(String deviceNo) {
        FallRadarPageQuery fallRadarPageQuery = new FallRadarPageQuery();
        fallRadarPageQuery.setDeviceNo(deviceNo);
        fallRadarPageQuery.setDeviceType(DmcDeviceTypeEnum.FALL_RADAR.getIndex());
        PageHelper.startPage(1, 1);
        List<DmcFallRadarVo> dmcCsunDoorVoList = dmcFallRadarLocationMapper
                .queryFallRadarListWithPage(fallRadarPageQuery);
        if (PubUtils.isNull(dmcCsunDoorVoList)) {
            return null;
        }
        return dmcCsunDoorVoList.get(0);
    }

    /**
     * delete csun door by id
     *
     * @param id the id
     * @return the int.
     */
    @Override
    public Integer deleteFallRadarById(Long id) {

        // 1.判断设备Id是否存在
        DmcDevice dmcDevice = dmcDeviceService.checkDeviceIsRadarByDeviceId(id);
        if (PublicUtil.isEmpty(dmcDevice)) {
            throw new DmcBizException(ErrorCodeEnum.DMC100610073, id);
        }
        // 2.判断紧急按钮是否已经绑定
        DmcFallRadarLocation dmcFallRadarLocation = dmcFallRadarLocationMapper
                .selectByPrimaryKey(id);
        if (PublicUtil.isNotEmpty(dmcFallRadarLocation)) {
            dmcFallRadarLocationMapper.deleteByPrimaryKey(id);
        }
        // 3.删除设备
        return dmcDeviceService.deleteDmcDeviceById(id);
    }

}
